package com.yc.fruitmarket.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yc.fruitmarket.biz.wishBiz;
import com.yc.fruitmarket.biz.impl.wishBizImpl;

@WebServlet("/wishController/*")
public class wishController extends BaseServlet {
	private wishBiz wb = new wishBizImpl();
	//查询心愿单数量
	public void getWishCount(HttpServletRequest req, HttpServletResponse resp) {
		String uid = req.getParameter("uid");
		if(!"".equals(uid)) {
			int count = wb.getWishCount(uid);
			this.send(resp, count);
		}else {
			return;
		}
	}
	//添加心愿单
	public void addWish(HttpServletRequest req, HttpServletResponse resp) {
		String uid = req.getParameter("uid");
		String pid = req.getParameter("pid");
		String price = req.getParameter("price");
		String num = req.getParameter("num");
		String inventory = req.getParameter("inventory");
		if("".equals(uid) || "".equals(pid) || "".equals(price)) {
			this.send(resp, -1);
			return;
		}else {
			int result = wb.addWish(uid, pid, price,num,inventory);
			this.send(resp, result);
		}
	}
	//根据uid查心愿单
	public void getWishsByUid(HttpServletRequest req, HttpServletResponse resp) {
		String uid = req.getParameter("uid");
		if("".equals(uid) || uid == null) {
			return;
		}
		List<Map<String, String>> list = wb.getWishsByUid(uid);
		if(list.size() > 0) {
			for(int i = 0; i < list.size(); i++) {
		 		list.get(i).put("checked", "true");
		 	}
		 	this.send(resp, list);
		}else {
			return;
		}
	}
	public void addCart(HttpServletRequest req, HttpServletResponse resp) {
		String wid = req.getParameter("wid");
		String uid = req.getParameter("uid");
		String pid = req.getParameter("pid");
		String wishnum = req.getParameter("wishnum");
		int result = wb.addCart(uid, pid, wishnum, wid);
		this.send(resp, result);
	}
	public void deleteWish(HttpServletRequest req, HttpServletResponse resp) {
		String wid = req.getParameter("wid");
		String uid = req.getParameter("uid");
		String pid = req.getParameter("pid");
		String wishnum = req.getParameter("wishnum");
		int result = wb.deleteWish(wid);
		this.send(resp, result);
	}
}
